package com.ficus.road.maintain.util;

import cn.hutool.core.lang.Pair;
import com.ficus.road.maintain.core.model.Coordinate;
import com.ficus.road.maintain.core.model.GpsHistory;
import com.ficus.road.maintain.core.model.GpsLocation;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.util.ArrayList;
import java.util.List;

public class LngLonUtil {

    static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    // π
    static double pi = 3.1415926535897932384626;
    // 长半轴
    static double a = 6378245.0;
    // 扁率
    static double ee = 0.00669342162296594323;

    static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }


    /**
     * 百度坐标系(BD-09)转WGS坐标
     *
     * @param lng 百度坐标纬度
     * @param lat 百度坐标经度
     * @return WGS84坐标数组
     */
    public static double[] bd09towgs84(double lng, double lat) {


        double[] gcj = bd09togcj02(lng, lat);
        double[] wgs84 = gcj02towgs84(gcj[0], gcj[1]);
        return wgs84;
    }

    /**
     * WGS坐标转百度坐标系(BD-09)
     *
     * @param lng WGS84坐标系的经度
     * @param lat WGS84坐标系的纬度
     * @return 百度坐标数组
     */
    public static double[] wgs84tobd09(double lng, double lat) {


        double[] gcj = wgs84togcj02(lng, lat);
        double[] bd09 = gcj02tobd09(gcj[0], gcj[1]);
        return bd09;
    }

    /**
     * 火星坐标系(GCJ-02)转百度坐标系(BD-09)
     *
     * @param lng 火星坐标经度
     * @param lat 火星坐标纬度
     * @return 百度坐标数组
     * @see google、高德——>百度
     */
    public static double[] gcj02tobd09(double lng, double lat) {


        double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
        double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
        double bd_lng = z * Math.cos(theta) + 0.0065;
        double bd_lat = z * Math.sin(theta) + 0.006;
        return new double[]{

                bd_lng, bd_lat};
    }

    /**
     * 百度坐标系(BD-09)转火星坐标系(GCJ-02)
     *
     * @param bd_lon 百度坐标纬度
     * @param bd_lat 百度坐标经度
     * @return 火星坐标数组
     * @see ——>谷歌、高德
     */
    public static double[] bd09togcj02(double bd_lon, double bd_lat) {


        double x = bd_lon - 0.0065;
        double y = bd_lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double gg_lng = z * Math.cos(theta);
        double gg_lat = z * Math.sin(theta);
        return new double[]{

                gg_lng, gg_lat};
    }

    /**
     * WGS84转GCJ02(火星坐标系)
     *
     * @param lng WGS84坐标系的经度
     * @param lat WGS84坐标系的纬度
     * @return 火星坐标数组
     */
    public static double[] wgs84togcj02(double lng, double lat) {


        if (out_of_china(lng, lat)) {


            return new double[]{

                    lng, lat};
        }
        double dlat = transformlat(lng - 105.0, lat - 35.0);
        double dlng = transformlng(lng - 105.0, lat - 35.0);
        double radlat = lat / 180.0 * pi;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
        double mglat = lat + dlat;
        double mglng = lng + dlng;
        return new double[]{

                mglng, mglat};
    }

    /**
     * GCJ02(火星坐标系)转GPS84
     *
     * @param lng 火星坐标系的经度
     * @param lat 火星坐标系纬度
     * @return WGS84坐标数组
     */
    public static double[] gcj02towgs84(double lng, double lat) {


        if (out_of_china(lng, lat)) {


            return new double[]{

                    lng, lat};
        }
        double dlat = transformlat(lng - 105.0, lat - 35.0);
        double dlng = transformlng(lng - 105.0, lat - 35.0);
        double radlat = lat / 180.0 * pi;
        double magic = Math.sin(radlat);
        magic = 1 - ee * magic * magic;
        double sqrtmagic = Math.sqrt(magic);
        dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
        dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
        double mglat = lat + dlat;
        double mglng = lng + dlng;
        return new double[]{

                lng * 2 - mglng, lat * 2 - mglat};
    }

    /**
     * 纬度转换
     *
     * @param lng
     * @param lat
     * @return
     */
    public static double transformlat(double lng, double lat) {


        double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 * Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 经度转换
     *
     * @param lng
     * @param lat
     * @return
     */
    public static double transformlng(double lng, double lat) {


        double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lng * pi) + 40.0 * Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 * Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

    /**
     * 判断是否在国内，不在国内不做偏移
     *
     * @param lng
     * @param lat
     * @return
     */
    public static boolean out_of_china(double lng, double lat) {


        if (lng < 72.004 || lng > 137.8347) {


            return true;
        } else if (lat < 0.8293 || lat > 55.8271) {


            return true;
        }
        return false;
    }

    public static Coordinate getFoot() {


        return null;
    }

    private static Double calculateDistance(GpsLocation l1, GpsLocation l2) {
        GlobalCoordinates g1 = new GlobalCoordinates(l1.getLatitude(), l1.getLongitude());
        GlobalCoordinates g2 = new GlobalCoordinates(l2.getLatitude(), l2.getLongitude());
        GeodeticCurve curve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, g1, g2);
        return curve.getEllipsoidalDistance();
    }

    public static Double calculateDistance(Coordinate l1, Coordinate l2) {
        GlobalCoordinates g1 = new GlobalCoordinates(Double.parseDouble(l1.getY()), Double.parseDouble(l1.getX()));
        GlobalCoordinates g2 = new GlobalCoordinates(Double.parseDouble(l2.getY()), Double.parseDouble(l2.getX()));
        GeodeticCurve curve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, g1, g2);
        return curve.getEllipsoidalDistance();
    }

    public static Double calculateDistance(GpsHistory history1, GpsHistory history2) {
        GlobalCoordinates g1 = new GlobalCoordinates(history1.getLatitude(), history1.getLongitude());
        GlobalCoordinates g2 = new GlobalCoordinates(history2.getLatitude(), history2.getLongitude());
        GeodeticCurve curve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.WGS84, g1, g2);
        return curve.getEllipsoidalDistance();
    }


    private static Double calculateDistance2(Coordinate l1, Coordinate l2) {
        double lat1 = Double.parseDouble(l1.getY());
        double lat2 = Double.parseDouble(l2.getY());
        double lng1 = Double.parseDouble(l1.getX());
        double lng2 = Double.parseDouble(l2.getX());

        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s * 1000;
        return s;
    }

    public static Coordinate getMiddle(Coordinate l1, Coordinate l2) {
        String x = ((Double.parseDouble(l1.getX()) + Double.parseDouble(l2.getX()))) / 2 + "";
        String y = ((Double.parseDouble(l1.getY()) + Double.parseDouble(l2.getY()))) / 2 + "";
        return Coordinate.builder().x(x).y(y).build();
    }

    public static List<Coordinate> gpsSampling(List<Coordinate> list, double threshold) {
        Pair<List<Coordinate>, Boolean> pair = recursive(list, threshold);
        while (pair.getValue()) {
            pair = recursive(pair.getKey(), threshold);
        }
        return pair.getKey();
    }


    public static Pair<List<Coordinate>, Boolean> recursive(List<Coordinate> list, double threshold) {
        List<Coordinate> res = new ArrayList<>();
        Boolean isContinue = false;
        for (int i = 0; i < list.size() - 1; i++) {
            Coordinate pre = list.get(i);
            Coordinate post = list.get(i + 1);
            Coordinate middle;
            Double distance = calculateDistance(pre, post);
//            Double aDouble = calculateDistance2(pre, post);
            res.add(pre);
            if (distance > threshold) {
                middle = getMiddle(pre, post);
                res.add(middle);
                isContinue = true;
            }
        }
        res.add(list.get(list.size() - 1));
        return Pair.of(res, isContinue);
    }

    /**
     * 经纬度校验,只校验正数 0-90.000000 0-180.000000 范围内
     * 经度longitude: (?:[0-9]|[1-9][0-9]|1[0-7][0-9]|180)\\.([0-9]{6})
     * 纬度latitude：  (?:[0-9]|[1-8][0-9]|90)\\.([0-9]{6})
     *
     * @return 合法则true
     */
    public static boolean checkItude(double longitude, double latitude) {
        return longitude >= 0 && longitude <= 180 && latitude >= 0 && latitude <= 90;
    }

    public static void main(String[] args) {
        double[] doubles = gcj02towgs84(121.778865, 31.036276833333332);
        System.out.println(doubles);
    }
}